package com.zzsy.carosshopprojects.activitys.home.mainpage.member.memberconsum;

import android.app.Activity;
import android.content.Context;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.bean.GetOilsListBean;
import com.zzsy.carosshopprojects.bean.MemberInfoBean;
import com.zzsy.carosshopprojects.bean.MemberResultBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.AlgorithmUtil;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import java.util.List;

import androidx.lifecycle.MutableLiveData;

public class MemberConsumMode extends BaseViewMode {
    private MutableLiveData<List<GetOilsListBean>> liveData;
    private MutableLiveData<MemberResultBean> live;

    public MutableLiveData<List<GetOilsListBean>> getLiveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    public MutableLiveData<MemberResultBean> getLive() {
        if (live == null)
            live = new MutableLiveData<>();
        return live;
    }

    /**
     * 获取可加油的数据
     *
     * @param context
     * @param productId
     */
    public void getOilsData(Context context, String productId) {
        RequestUtils.OilListByStoreIdAndProductId(context, DataUtil.getInstance().getStoreId(), productId, new MyObserver<List<GetOilsListBean>>(context, false) {
            @Override
            public void onSuccess(List<GetOilsListBean> result) {
                liveData.postValue(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }

    /**
     * 根据UUID查询会员信息
     *
     * @param context
     * @param uuid
     * @param
     */
    public void MemberByUuid(Activity context, String uuid) {
        RequestUtils.MemberByUuid(context, uuid, DataUtil.getInstance().getCompanyId(), new MyObserver<MemberInfoBean>(context) {
            @Override
            public void onSuccess(MemberInfoBean bean) {
                String memberId = bean.getMemberId();
                if (StringUtils.isEmpty(memberId)) {
                    ToastUtils.showToast(context.getApplicationContext(), context.getApplicationContext().getResources().getString(R.string.thisCardNoMember));
                    context.finish();
                    return;
                }
                String parentId = bean.getParentId();
                String memberType = "";
                if (!StringUtils.isEmpty(parentId)) {
                    if ("0".equals(parentId)) {
                        memberType = "1";
                    } else {
                        memberType = "2";
                    }
                } else {
                    ToastUtils.showToast(context.getApplicationContext(), context.getApplicationContext().getResources().getString(R.string.CardError));
                    context.finish();
                    return;
                }
                String memberCardId = bean.getMemberCardId();
                if (StringUtils.isEmpty(memberCardId)) {
                    memberCardId = "";
                }
                //会员姓名
                String realName = bean.getRealName();
                if (StringUtils.isEmpty(realName)) {
                    realName = "暂未设置姓名";
                }

                //会员电话号
                String phone = bean.getPhone();
                if (StringUtils.isEmpty(phone)) {
                    phone = "";
                }

                //该参数只是为了显示当前会员属于什么油品的油品列表
                String memberOilsType = bean.getProductId();
                if (StringUtils.isEmpty(memberOilsType)) {
                    memberOilsType = "";
                }

                //充值余额
                String paySwitch = bean.getPaySwitch();
                if (StringUtils.isEmpty(paySwitch)) {
                    paySwitch = "";
                }
                //密码
                String password = bean.getPassword();
                if (StringUtils.isEmpty(password)) {
                    password = "";
                }
                String rechargeBalance = "";
                if ("1".equals(memberType)) {
                    rechargeBalance = bean.getBalance();
                    if (StringUtils.isEmpty(rechargeBalance)) {
                        rechargeBalance = "0";
                    }
                } else if ("2".equals(memberType)) {
                    /**
                     * 副卡额度
                     * */
//                    String payLimit = bean.getPayLimit();
//                    String useAmount = bean.getUseAmount();
//                    if (StringUtils.isEmpty(payLimit) || StringUtils.isEmpty(useAmount)) {
//                        ToastUtils.showToast(context, context.getResources().getString(R.string.SubCardError));
//                        context.finish();
//                        return;
//                    }
//                    rechargeBalance = ConverterHelper.toBigDecimal(payLimit).subtract(ConverterHelper.toBigDecimal(useAmount)).toString();
//                    if (StringUtils.isEmpty(rechargeBalance)) {
//                        rechargeBalance = "0";
//                    }
                    rechargeBalance = bean.getBalance();
                    if (StringUtils.isEmpty(rechargeBalance)) {
                        rechargeBalance = "0";
                    }
                }
                if (!StringUtils.isEmpty(DataUtil.getInstance().getStoreId())) {
                    getOilsData(context, memberOilsType);
                }
                MemberResultBean memberResultBean = new MemberResultBean();
                memberResultBean.setMemberCardId(memberCardId);
                memberResultBean.setMemberId(memberId);
                memberResultBean.setMemberOilsType(memberOilsType);
                memberResultBean.setMemberType(memberType);
                memberResultBean.setParentId(parentId);
                memberResultBean.setPassword(password);
                memberResultBean.setPaySwitch(paySwitch);
                memberResultBean.setPhone(phone);
                memberResultBean.setRealName(realName);
                memberResultBean.setRechargeBalance(AlgorithmUtil.getAmount(rechargeBalance, 100));
                live.postValue(memberResultBean);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context.getApplicationContext(), errorMsg);
            }
        });
    }
}
